<?php

class Technical_YearController extends Zend_Controller_Action
{

    public function init(){  
    }
    public function indexAction(){
    	$years = new Application_Model_Dao_Year();
    	$result = $years->getAllYear();
        $this->view->years = $result;
    	if($result == null){
			$this->view->empty = "Brak  roku studiów w bazie.";
		}else{
			$this->view->empty = "";
		}
        $session = new Zend_Session_Namespace('identity');
		$designation = $this->_getParam('designation');
        if(isset($session->statusdel) && $this->_getParam('statusdelete') == 'true') {
        	$this->view->delete = "Rekord \"".$designation."\" został poprawnie usunięty.";
			unset($session->statusdel);	
        }else{
        	$this->view->delete = "";
        }
    }

    public function addAction()
    {
    	$form = new Application_Form_Year();
    	$form->appendValidationEngineFiles($this->view); 
    	$polish = BH_TranslatePL::getPolishTranslation();
  		$translate = new Zend_Translate('array', $polish, 'pl');
  		$form->setTranslator($translate);
    	
    	$form->submit->setLabel('Dodaj nowy rok');
		$this->view->YearForm = $form;
    	
    	$request = $this->getRequest();
    	if ($request->isPost()) {
			 if ($form->isValid($request->getPost())) {
    			$year = new Application_Model_Entities_Year();
    			$year->setFaculty( $form->getValue('faculty_id') );
    			$year->setMajor( $form->getValue('major_id') );
    			$year->setNumberStudentYear( $form->getValue('numberStudentYear') );
    			$year->setNumberGroupLecture( $form->getValue('numberGroupLecture') );
    			$year->setAbundanceGroup( $form->getValue('abundanceGroup') );
    			$year->setTypeStudy( $form->getValue('typeStudy_id') );
    			$year->setDegreeStudy( $form->getValue('degreeStudy_id') );
    			$yearDao = new Application_Model_Dao_Year();
    			$yearDao->saveYear($year);
    			$this->_helper->redirector('index');
    		} else {
    				//
    		}
    	}
    }
 public function editAction()
    {
    	$form = new Application_Form_Year();
    	$form->appendValidationEngineFiles($this->view); 
    	$form->submit->setLabel('Zapisz zmiany');
		$this->view->YearEditForm = $form;
		
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$year = new Application_Model_Entities_Year();
				$year->setId($form->getValue('id'));
    			$year->setFaculty( $form->getValue('faculty_id') );
    			$year->setMajor( $form->getValue('major_id') );
    			$year->setNumberStudentYear( $form->getValue('numberStudentYear') );
    			$year->setNumberGroupLecture( $form->getValue('numberGroupLecture') );
    			$year->setAbundanceGroup( $form->getValue('abundanceGroup') );
    			$year->setTypeStudy( $form->getValue('typeStudy_id') );
    			$year->setDegreeStudy( $form->getValue('degreeStudy_id') );
    			$yearDao = new Application_Model_Dao_Year();
				$yearDao->updateYear($year);
				$this->_helper->redirector('index');
			} else
				$form->populate($formData);
		} else {
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$yearDao = new Application_Model_Dao_Year();
				$form->populate($yearDao->getYearByID($id)); 
			} 
		}
    }
public function deleteAction(){

		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del');
			if ($del == 'Tak, usuń rekord') {
				$id = $request->getPost('id');
				$designation = $request->getPost('designation');
				$year = new Application_Model_Entities_Year();
				$year->setId($id);
				$yearDao = new Application_Model_Dao_Year();
				$yearDao->deleteYear($year);
				$session = new Zend_Session_Namespace('identity');
    			$session->statusdel = 'true';
				$this->_helper->redirector( 'index', 'year', 'technical', array('statusdelete' => 'true', 'designation' => $designation));
			}else
				$this->_helper->redirector( 'index');
		}
		if($this->_getParam('jsflag') == 'false' ){
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$yearDao = new Application_Model_Dao_Year();
				$result = $yearDao->getRowYear($id);
				$this->view->years = $result;
			}
		}
		
		if($this->_getParam('jsflag') == 'true' ){
			$id = $this->_getParam('id', 0);
				$year = new Application_Model_Entities_Year();
				$year->setId($id);
				$yearDao = new Application_Model_Dao_Year();
				$yearDao->deleteYear($year);
			//$this->_helper->redirector('index');
		}
	}
	
	public function getAction() {
	    $request = $this->getRequest();
    	if ($request->isPost()) {
    		$facultyParam = $request->getPost('facultyparam');
    		if($facultyParam == 'true') {
    			$faculty = $request->getPost('faculty');
    			$cathedralDao = new Application_Model_Dao_Cathedral();
				$cathedrals = $cathedralDao->getToSelectWhere($faculty);
    			echo json_encode($cathedrals);
    		
    		}
		}
	
	}
}